//
//  BJWebContentViewController.m
//
//
//  Created by 2023 on 2023/4/21.
//  Copyright © 2023 i. All rights reserved.
//  加载网页内容

#import "BJWebContentViewController.h"
#import <WebKit/WebKit.h>
#import "SDWebView.h"

@interface BJWebContentViewController ()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler,UIGestureRecognizerDelegate>
@property (nonatomic, strong)SDWebView *wkWebView;
@property (nonatomic, strong) UIProgressView *progressView;
@property (nonatomic, copy) NSString *url;
@property (nonatomic,assign)BOOL hadNavBar;


@end

@implementation BJWebContentViewController

/// 加载网页内容
/// @param title 标题
/// @param url 网址或者富文本
-(instancetype)initWithTitle:(NSString *)title url:(NSString *)url{
    self = [super init];
    if(self){
        self.title = title;
        self.url = url;
        self.view.backgroundColor = [UIColor whiteColor];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.customNaviBar.hidden = NO;

    self.naviTitle = self.title;
    [self initUI];
}




-(void)initUI{
    
    
    self.wkWebView = [[SDWebView alloc] initWithFrame:CGRectMake(0,kNavBarHeight, kScreenW,kScreenH-kNavBarHeight)];
    self.wkWebView.backgroundColor = [UIColor whiteColor];
    self.wkWebView.opaque = NO;
    self.wkWebView.userInteractionEnabled = YES;
    self.wkWebView.scrollView.bounces = NO;
    self.wkWebView.navigationDelegate = self;

    [self.wkWebView sizeToFit];
    [self.view addSubview:self.wkWebView];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [self.wkWebView loadRequest:request];
    [self.wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];

}


//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
//    NSLog(@"开始加载网页");
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
//    NSLog(@"加载完成");
}

//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
//    NSLog(@"加载失败");

}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
    
    }else if ([keyPath isEqualToString:@"title"])
    {
        if (object == self.wkWebView) {
        }
        else
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];

        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

#pragma mark - WKScriptMessageHandler

//WKScriptMessageHandler代理方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
//    NSLog(@"WKScriptMessage:%@",message);
}


-(void)goBack{
    // 返回按钮
    NSArray *viewcontrollers=self.navigationController.viewControllers;
     if (viewcontrollers.count>1) {
         //push方式
         if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self){
             [self.navigationController popViewControllerAnimated:YES];
         }
     }else{
         //present方式
         [self dismissViewControllerAnimated:YES completion:nil];
     }
 

}
- (void)dealloc {
    [self.wkWebView removeObserver:self forKeyPath:@"title"];
    [[NSNotificationCenter defaultCenter]removeObserver:self];

}



@end
